package db

import (
	"fmt"
	"github.com/seefan/gossdb"
	"github.com/seefan/gossdb/conf"
	"os"
	"strconv"
)

var (
	MainSsdbClient *gossdb.Client
)

func InitSsdb() (err error) {
	var (
		host             string
		port             int
		minPoolSize      int
		maxPoolSize      int
		acquireIncrement int
		connectors       *gossdb.Connectors
	)
	host = os.Getenv("SSDB_HOST")
	port, _ = strconv.Atoi(os.Getenv("SSDB_PORT"))
	minPoolSize, _ = strconv.Atoi(os.Getenv("SSDB_MINSIZE"))
	maxPoolSize, _ = strconv.Atoi(os.Getenv("SSDB_MAXSIZE"))
	acquireIncrement, _ = strconv.Atoi(os.Getenv("SSDB_ACQUIRE"))

	if connectors, err = gossdb.NewPool(&conf.Config{
		Host:             host,
		Port:             port,
		MinPoolSize:      minPoolSize,
		MaxPoolSize:      maxPoolSize,
		AcquireIncrement: acquireIncrement,
	}); err != nil {
		fmt.Printf("NewPool err: %s", err)
		return
	}
	if MainSsdbClient, err = connectors.NewClient(); err != nil {
		fmt.Printf("NewClient err: %s", err)
		return
	}
	//defer MainSsdbClient.Close()
	return
}
